--
-- Author: ping.wang
-- Date: 2018-10-15 20:05:44
--
-- AuctionNormalBuyView
-- 普通商品购买界面

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)
local AuctionNormalBuyFixedRender = import(".renders.AuctionNormalBuyFixedRender")
local AuctionBuyListRender = import(".renders.AuctionBuyListRender")
local IconRender = require("app.widget.renders.IconRender")

ClassRef.VIEW_ROOT_RES = Res.AuctionNormalBuyScene

function ClassRef:init()
    self._soldListView = self._ui["Panel/Panel_list/ListView"]
    self._ui["Panel/Panel_buy/Button_close"]:addClickEventListener(function()
        self:closeView()
    end)
    self._buy1 = AuctionNormalBuyFixedRender.create(self._ui["Panel/Panel_buy/Node_buy_1"])

    --右侧部分
    self._icon = IconRender.create(self._ui["Panel/Panel_buy/icon"])
    self._icon:setTipEnable(true)
    local itemData = self._data.itemData
    self._ui["Panel/Panel_buy/name"]:setString(itemData:getName(true))
    self._ui["Panel/Panel_buy/name"]:setTextColor( Enums.QualityColor[ itemData:getQuality() ] )
    local classifyInfo =  GD:queryAuctionshopById(itemData:getSubMenu())
    if classifyInfo then
        self._ui["Panel/Panel_buy/type"]:setString(L(classifyInfo.name))
    end
    self._icon:setData(itemData)
    self._icon:setNumBgVisible(false)

    self._buy1:setData(self._data)

    self._allSoldRenders = {}
end

--通过服务器发送回来的前10条记录刷新面板
function ClassRef:refreshList(data)
    self._soldListView:removeAllChildren()
    self._allSoldRenders = {}
    self._buy1:setSoldListData(data)
    if #data > 0 then
        --这里要计算出来每一条需要购买多少个
        for i = 1, #data do
            local render = AuctionBuyListRender.create()
            render:setData({idx = i, price = data[i]:getSellPrice(), stock = data[i]:getCount(), buyNum = (i == 1 and 1 or 0)})
            self._soldListView:pushBackCustomItem(render)
            table.insert(self._allSoldRenders, render)
        end
    end
    self._soldListView:refreshView()
end

--右侧选中数量需要更新左侧list相关数量信息
function ClassRef:refreshListByData(buyTotalNum)
    local needBuyNum = buyTotalNum
    local tempNum = 0
    for i = 1, #self._allSoldRenders do
        tempNum = self._allSoldRenders[i]:getTotalCount()
        if needBuyNum > 0 then
            if needBuyNum >= tempNum then
                self._allSoldRenders[i]:setBuyNum(tempNum)
                needBuyNum = needBuyNum - tempNum
            else
                self._allSoldRenders[i]:setBuyNum(needBuyNum)
                needBuyNum = 0
            end
        else
            self._allSoldRenders[i]:setBuyNum(0)
        end
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
    self._buy1:onBtnClick(self._onOperate)
end

return ClassRef
